﻿layui.use(["form", 'table', 'dropdown'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var dropdown = layui.dropdown;
    window.Show = function Show() {
        var outboundState = $("#outboundState").val()
        var token = localStorage.getItem("token")
        var model = JSON.stringify({
            outboundNo: $("#outboundNo").val(),
            outboundType: $("#outboundType").val(),
            outboundState: parseInt(outboundState || 0),
        })
        //table.render({
        //    elem: '#test',
        //    url: '../Warehouse/OutBoundShowDo', // 此处为静态模拟数据，实际使用时需换成真实接口
        //    toolbar: '#toolbarDemo',
        //    method: 'post', // 设置为POST请求
        //    defaultToolbar: ['filter', 'exports', 'print', { // 右上角工具图标
        //        title: '提示',
        //        layEvent: 'LAYTABLE_TIPS',
        //        icon: 'layui-icon-tips',
        //        onClick: function (obj) { // 2.9.12+
        //            layer.alert('自定义工具栏图标按钮');
        //        }
        //    }],
        //    where: {
        //       model:model, // 假设这是一个输入框的值，表示出库单号
        //       token:token// 假设这是一个输入框的值，表示产品名称
        //    },
        //    height: 'full-35', // 最大高度减去其他容器已占有的高度差
        //    css: [ // 重设当前表格样式
        //        '.layui-table-tool-temp{padding-right: 145px;}'
        //    ].join(''),
        //    cellMinWidth: 80,
        //    totalRow: true, // 开启合计行
        //    page: true,
        //    cols: [[
        //        { field: 'outboundid', type: 'checkbox', fixed: 'left' },
        //        { field: 'outboundNo', title: '订单号', width: 80 },
        //        { field: 'productName', title: '产品名称', width: 120 },
        //        { field: 'productNo', title: '产品编号', width: 120 },
        //        { field: 'batch', title: '批次', width: 120 },
        //        { field: 'productSpec', title: '规格', width: 120 },
        //        { field: 'outStorageNum', title: '数量', width: 120 },
        //        { field: 'binLocationName', title: '库位', width: 120 },
        //        { field: 'outboundType', title: '出库类型', width: 120 },
        //        { field: 'productsSalesTotal', title: '出库数', width: 120 },
        //        { field: 'customerName', title: '客户', width: 120 },
        //        { field: 'singleMaker', title: '制单人', width: 120 },
        //        {
        //            field: 'outboundState', title: '出库状态', width: 80, templet: function (d) {
        //                if (d.outboundState == 1) {
        //                    return d.outboundState = '待审核'
        //                }
        //                else if (d.outboundState == 2) {
        //                    return d.outboundState = '审核成功'
        //                }
        //                else {
        //                    return d.outboundState = '审核失败'
        //                }
        //            }
        //        },
        //        { field: 'examinePeople', title: '审核人', width: 120 },
        //        { field: 'examineTime', title: '审核时间', width: 120 },
        //        {
        //            fixed: 'right', title: '操作', width: 150, templet: function (d) {
        //                return '<a href="#" onclick="FanUpdate(' + d.outboundid + ')">编辑</a>' +
        //                    '<a href="#" onclick="IsDel(' + d.outboundid + ')">删除</a>';
        //            }
        //        }
        //    ]],
        //})
        $.ajax({
            url: '../Warehouse/OutBoundShowDo',
            type: 'post',
            //contentType: 'application/json',
            async: false,
            data: { model: model, token: token },
            success: function (res) {
                res = JSON.parse(res);

                // 更新总记录数
                totalCount = res.totalCount;
                //总页数
                sum = res.totalPage;
                console.log(res)
                // 处理数据并渲染表格
                var cols = [
                    [
                        { field: 'outboundid', type: 'checkbox', fixed: 'left' },
                        { field: 'outboundNo', title: '订单号', width: 80 },
                        { field: 'productName', title: '产品名称', width: 120 },
                        { field: 'productNo', title: '产品编号', width: 120 },
                        { field: 'batch', title: '批次', width: 120 },
                        { field: 'productSpec', title: '规格', width: 120 },
                        { field: 'outStorageNum', title: '数量', width: 120 },
                        { field: 'binLocationName', title: '库位', width: 120 },
                        { field: 'outboundType', title: '出库类型', width: 120 },
                        { field: 'productsSalesTotal', title: '出库数', width: 120 },
                        { field: 'customerName', title: '客户', width: 120 },
                        { field: 'singleMaker', title: '制单人', width: 120 },
                        {
                            field: 'outboundState', title: '出库状态', width: 80, templet: function (d) {
                                if (d.outboundState == 1) {
                                    return d.outboundState = '待审核'
                                }
                                else if (d.outboundState == 2) {
                                    return d.outboundState = '审核成功'
                                }
                                else {
                                    return d.outboundState = '审核失败'
                                }
                            }
                        },
                        { field: 'examinePeople', title: '审核人', width: 120 },
                        { field: 'examineTime', title: '审核时间', width: 120 },                        
                        {
                            fixed: 'right', title: '操作', width: 150, templet: function (d) {
                                return '<a href="#" class="layui-btn layui-btn-xs layui-btn-primary"  onclick="FanUpdate(' + d.outboundid + ')">编辑</a>' +
                                    '<a href="#" class="layui-btn layui-btn-xs layui-btn-danger" onclick="IsDel(' + d.outboundid + ')">删除</a>';
                            }
                        }
                    ]
                ];
                table.render({
                    elem: '#test',
                    data: res,
                    cols: cols,
                    page: true,
                    limit: 5,
                    limits: [5, 10, 15, 20],
                });
            },
        });
    }
    window.IsDel = function IsDel(outboundid) {
        var token = localStorage.getItem("token")
        var model = JSON.stringify({
            outboundid: outboundid
        })
        layer.confirm("确认删除吗", function () {
            $.ajax({
                url: '../Warehouse/OutBoundIsdelDo',
                data: { model: model, token: token },
                type: 'post',
                success: function (res) {
                    console.log(res);
                    var res = JSON.parse(res)
                    if (res > 0) {
                        layer.msg("删除成功", { icon: 1 })
                        Show();
                    }
                    else {
                        layer.msg("删除失败", { icon: 2 })
                    }
                }
            })
        })
    }
    window.IsdelAll = function IsdelAll() {
        var checkStatus = table.checkStatus('test');
        var checkedData = checkStatus.data;
        var outboundid = [];
        for (var i = 0; i < checkedData.length; i++) {
            outboundid.push(checkedData[i].outboundid);
        }
        if (outboundid == 0) {
            layer.msg("至少选择一条数据", { icon: 2 })
            return;
        }
        var model = JSON.stringify({
            ints: outboundid
        });
        console.log(model);
        var token = localStorage.getItem("token")
        layer.confirm("确认删除吗", function () {
            $.ajax({
                url: '../Warehouse/OutBoundIsdelAllDo',
                data: { model: model, token: token },
                type: 'post',
                success: function (res) {
                    console.log(res);
                    var res = JSON.parse(res)
                    if (res > 0) {
                        layer.msg("删除成功", {icon: 1})
                        Show();
                    }
                    else {
                        layer.msg("删除失败", {icon:2})
                    }
                }
            })
        })
    }
    window.Output = function Output() {
        var data = [];

        // 直接获取表格正文数据并添加到data数组，跳过表头（thead）获取
        $('table.layui-table tbody tr').each(function () {
            var rowData = [];
            $(this).find('td').each(function () {
                rowData.push($(this).text());
            });
            data.push(rowData);
        });

        // 将数据转换为Worksheet对象
        var worksheet = XLSX.utils.aoa_to_sheet(data);

        // 创建一个新的Workbook对象，并将Worksheet添加进去
        var workbook = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");

        // 将Workbook对象转换为二进制数据
        var wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' });

        // 将二进制数据转换为Blob对象
        function s2ab(s) {
            var buf = new ArrayBuffer(s.length);
            var view = new Uint8Array(buf);
            for (var i = 0; i < s.length; i++) {
                view[i] = s.charCodeAt(i) & 0xff;
            }
            return new Blob([buf], { type: 'application/octet-stream' });
        }
        var blob = s2ab(wbout);

        // 创建一个下载链接并触发点击事件来下载文件
        var url = URL.createObjectURL(blob);
        var link = document.createElement('a');
        link.href = url;
        link.download = '出库Outbound.xlsx';
        link.click();

        // 释放URL对象
        URL.revokeObjectURL(url);
    }
    window.FanUpdate = function FanUpdate(outboundid) {
        localStorage.setItem("outboundid", outboundid)
        location.href = '/Warehouse/OutboundUpdate'
    }
    window.FanUpdateAll = function FanUpdateAll() {
        var checkStatus = table.checkStatus('test');
        var checkedData = checkStatus.data;
        var outboundid = [];
        for (var i = 0; i < checkedData.length; i++) {
            outboundid.push(checkedData[i].outboundid);
        }
        localStorage.setItem("outboundid", outboundid[0])
        location.href = '/Warehouse/OutboundUpdate'
    }
    $(function () {
        Show();
    })
});